package GUI;

    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.image.BufferedImage;
    import java.io.FileOutputStream;

    import javax.media.Buffer;
    import javax.media.CaptureDeviceInfo;
    import javax.media.CaptureDeviceManager;
    import javax.media.Manager;
    import javax.media.MediaLocator;
    import javax.media.Player;
    import javax.media.control.FrameGrabbingControl;
    import javax.media.format.VideoFormat;
    import javax.media.util.BufferToImage;
    import javax.swing.JButton;

    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGEncodeParam;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.util.Vector;

    public class SwingCapture extends Panel implements ActionListener
    {
      public static Player player = null;
      public CaptureDeviceInfo di = null;
      public MediaLocator ml = null;
      public JButton capture = null;
      public Buffer buf = null;
      public Image img = null;
      public VideoFormat vf = null;
      public BufferToImage btoi = null;
      public ImagePanel imgpanel = null;

      public SwingCapture()
      {
        setLayout(new BorderLayout());
        setSize(400,400);

        imgpanel = new ImagePanel();
        capture = new JButton("Capture");
        capture.addActionListener(this);

        VideoFormat vf = new VideoFormat("YUV",
                                                 new Dimension(352,288),
                                                 460800, //NOT_SPECIFIED, //length
                                                 VideoFormat.byteArray, (float) 30.0);

        Vector devices = CaptureDeviceManager.getDeviceList(vf);
        if (devices.size() < 1) {
            System.err.println("Nenhum dispositivo encontrado.");
            System.exit(-1);
        }
        di = (CaptureDeviceInfo) CaptureDeviceManager.getDeviceList(vf).get(0);
        ml = di.getLocator();

        try
        {
          player = Manager.createRealizedPlayer(ml);
          player.start();
          Component comp;

          if ((comp = player.getVisualComponent()) != null)
          {
            add(comp,BorderLayout.NORTH);
          }
          add(capture,BorderLayout.CENTER);
          add(imgpanel,BorderLayout.SOUTH);
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }

      public static void main(String[] args)
      {
        Frame f = new Frame("SwingCapture");
        SwingCapture cf = new SwingCapture();

        f.addWindowListener(new WindowAdapter() {

      @Override
      public void windowClosing(WindowEvent e) {
          playerclose();
          System.exit(0);}});

        f.add("Center",cf);
        f.pack();
        f.setSize(new Dimension(400,400));
        f.setVisible(true);
      }


      public static void playerclose()
      {
        player.close();
        player.deallocate();
      }


      public BufferedImage captura()
      {
          // Grab a frame
          FrameGrabbingControl fgc = (FrameGrabbingControl)
          player.getControl("javax.media.control.FrameGrabbingControl");
          buf = fgc.grabFrame();

          // Convert it to an image
          btoi = new BufferToImage((VideoFormat)buf.getFormat());
          img = btoi.createImage(buf);

          // show the image
          return imageToBufferedImage(img);
          // save image
          
        
      }

      public static void saveJPG(Image img, String s)
      {
        BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bi.createGraphics();
        g2.drawImage(img, null, null);

        FileOutputStream out = null;
        try
        {
          out = new FileOutputStream(s);
        }
        catch (java.io.FileNotFoundException io)
        {
          System.out.println("File Not Found");
        }

        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
        param.setQuality(0.5f,false);
        encoder.setJPEGEncodeParam(param);

        try
        {
          encoder.encode(bi);
          out.close();
        }
        catch (java.io.IOException io)
        {
          System.out.println("IOException");
        }
      }

    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

     public static BufferedImage imageToBufferedImage(Image im) {
        BufferedImage bi = new BufferedImage
        (im.getWidth(null),im.getHeight(null),BufferedImage.TYPE_INT_RGB);
        Graphics bg = bi.getGraphics();
        bg.drawImage(im, 0, 0, null);
        bg.dispose();
        return bi;
    }

    }





 